package com.frxuexi.user.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.paging.AskDataSource
import com.frxuexi.user.repository.AskRepository
import com.frxuexi.user.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class AskViewModel @Inject constructor(private val askRepository: AskRepository):BaseViewModel<AskIntent>() {
    override fun handleIntent(it: AskIntent) {
        when(it){
            is AskIntent.commitAsk->{
                roomRequest(askRepository.commitAsk(it.askLeave))
            }
            is AskIntent.getUserAskList->{
//                roomRequest(askRepository.getUserAskList(it.uid) as Flow<Any>)
            }
            is AskIntent.getNoApprove->{
                roomRequest(askRepository.getNoApprove() as Flow<Any>)
            }
            is AskIntent.approve->{
                roomRequest(askRepository.approve(it.askLeave))
            }
        }
    }

    fun getData(uid:Int)=Pager(PagingConfig(pageSize = 1)){
        AskDataSource(askRepository,uid,10)
    }.flow.cachedIn(viewModelScope)
}